package net.gyapp.markim.module.contact.adapter;

import android.text.TextUtils;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.entity.ConversationEntity;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.contact.vo.GroupListVo;
import net.gyapp.markim.module.contact.vo.UserVo;

import java.util.List;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/12.
 * Desc:
 */
public class SearchResultAdapter extends BaseQuickAdapter<Object, BaseRecyclerHolder> {
    public final static int TYPE_USER = R.layout.item_search_result_user;
    public final static int TYPE_GROUP = R.layout.item_search_result_group;

    public SearchResultAdapter() {
        super(null);
    }

    public void setAdapterData(List<Object> list) {
        setNewData(list);
    }

    @Override
    protected int getDefItemViewType(int position) {
        Object o = getData().get(position);
        if (o instanceof UserVo.UserInfoBean) {
            return TYPE_USER;
        } else {
            return TYPE_GROUP;
        }
    }

    @Override
    protected BaseRecyclerHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_USER:
            case TYPE_GROUP:
                return createBaseViewHolder(getItemView(viewType, parent));
        }
        return super.onCreateDefViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_USER:
                convertUser(holder, getItem(position - getHeaderLayoutCount()));
                break;
            case TYPE_GROUP:
                convertGroup(holder, getItem(position - getHeaderLayoutCount()));
                break;
        }
    }

    private void convertGroup(BaseRecyclerHolder helper, Object item) {
        if (item instanceof GroupListVo.FindGroupsBean) {
            GroupListVo.FindGroupsBean bean = (GroupListVo.FindGroupsBean) item;
            helper.setImageManager(mContext, R.id.iv_header, bean.groupAvatar, R.drawable.placeholder_group_header);
            helper.setText(R.id.tv_name, TextUtils.isEmpty(bean.groupName) ? bean.groupNumber : bean.groupName);
            helper.setText(R.id.tv_num, String.valueOf(bean.userNumber));
            helper.addOnClickListener(R.id.tv_apply);
            ConversationEntity entity = ImHelper.generateConversation(bean.groupNumber);
            if(entity == null) {
                helper.setText(R.id.tv_apply, "申请加入");
            }else {
                helper.setText(R.id.tv_apply, "点击进入");
            }
//            if (bean.groupStatus == 1 || bean.groupStatus == 4) { //0-正常, 1-退出, 2-屏蔽,3-禁言，4-申请入群
//                helper.setText(R.id.tv_apply, "申请加入");
//            } else {
//                helper.setText(R.id.tv_apply, "点击进入");
//            }
        }
    }

    private void convertUser(BaseRecyclerHolder helper, Object item) {
        if (item instanceof UserVo.UserInfoBean) {
            UserVo.UserInfoBean bean = (UserVo.UserInfoBean) item;
            helper.setImageManager(mContext, R.id.iv_header, bean.userAvatar, R.drawable.placeholder_header);
            helper.setText(R.id.tv_name, TextUtils.isEmpty(bean.nick) ? bean.userName : bean.nick);
            helper.addOnClickListener(R.id.tv_details);

        }
    }


    @Override
    protected void convert(BaseRecyclerHolder helper, Object item) {

    }
}
